<?php
/**
 * CRegistry class file.
 * 类工厂, 存放对象.
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CRegistry.php 1 2011-02-10 16:51:30Z huan.song $
 * @package system.helpers
 * @since 2.0
 */
class CRegistry
{
	/**
	 * @var array 存放对象.
	 */
	private static $_instances = array();
	
	/**
	 * 获取对象.
	 * <pre>
	 * $classname = 'CView';
	 * $instance = CRegistry::get($classname);
	 * </pre>
	 * @param string $index 对象名称.
	 * @return Object 实例化对象.
	 * @see set
	 */
	public static function get($index)
	{
		if (!self::has($index)) 
			throw new CHelperException('CRegistry:get No entry is registered for key', array('{index}' => $index));
		
		return self::$_instances[$index];
	}
	
	/**
	 * 设置对象.
	 * <pre>
	 * $classname = 'CView';
	 * $instance = new CView();
	 * CRegistry::set($classname, $instance);
	 * </pre>
	 * @param string $index 对象名称.
	 * @param Object $instance 实例化对象.
	 * @see get
	 * @return void.
	 */
	public static function set($index, $instance = null)
	{
		self::$_instances[$index] = $instance;
	}
	
	/**
	 * 判读某个对象是否存在.
	 * <pre>
	 * $classname = 'CView';
	 * $bool = CRegistry::has($classname);
	 * </pre>
	 * @param string $index 对象名称.
	 * @return boolean 是否存在.
	 */
	public static function has($index)
	{
		return array_key_exists($index, self::$_instances);
	}
}
